package edu.isistan.matching.adapters;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;


import edu.isistan.matching.structures.elements.TypeElement;
import edu.isistan.metamodel.DataType;

public class IntegerAdapter extends PrimitiveTypeAdapter {

	private static Set<String> fullyCompatible = new HashSet<String>();
	
	private static Set<String> loosyCompatible = new HashSet<String>();
	
	
	static Hashtable<String, String> codigosHash = new Hashtable<String, String>();
	
	static {
		fullyCompatible.add("integer");
		fullyCompatible.add("int");
		fullyCompatible.add("unsignedInt");
		fullyCompatible.add("java.lang.Interger");
		fullyCompatible.add("java.lang.Double");
		fullyCompatible.add("java.lang.Long");		
		
		loosyCompatible.add("byte");
		loosyCompatible.add("unsignedByte");
		loosyCompatible.add("decimal");
		loosyCompatible.add("double");
		loosyCompatible.add("float");
		loosyCompatible.add("long");
		loosyCompatible.add("short");
		loosyCompatible.add("unsignedShort");
		loosyCompatible.add("string");
		loosyCompatible.add("java.lang.Short");	
		
		codigosHash.put("java.lang.Number",  CODE_MASK + ".intValue()");
		
		codigosHash.put("java.lang.String", "new Integer(" + CODE_MASK + ")");
		codigosHash.put("string", " Integer.valueOf(" + CODE_MASK + ").intValue()");
		
		codigosHash.put("java.lang.Long",  CODE_MASK + ".intValue()");
		codigosHash.put("long", "new Long(" + CODE_MASK + ").intValue()");

		codigosHash.put("java.lang.Double",  CODE_MASK + ".intValue()");
		codigosHash.put("double", "new Double(" + CODE_MASK + ").intValue()");
		
		codigosHash.put("java.lang.Float",  CODE_MASK + ".intValue()");
		codigosHash.put("float", "new Float(" + CODE_MASK + ").intValue()");
		
		codigosHash.put("java.lang.Short",  CODE_MASK + ".intValue()");
		codigosHash.put("short", "new Short(" + CODE_MASK + ").intValue()");
		
		codigosHash.put("java.lang.Byte",  CODE_MASK + ".intValue()");
		codigosHash.put("byte", "new Byte(" + CODE_MASK + ").intValue()");
		
		codigosHash.put("java.math.BigInteger",  CODE_MASK + ".intValue()");
		codigosHash.put("java.math.BigDecimal",  CODE_MASK + ".intValue()");
	}
	
	@Override
	public Set<String> getFullyCompatibles() {
		return fullyCompatible;
	}

	@Override
	public Set<String> getLoosyCompatibles() {
		return loosyCompatible;
	}

	@Override
	public Object toValue(DataType element) {
		PrimitiveTypeAdapter adapter = AdapterFactory.getAdapter(element);
		return adapter.valueof(getValue());
	}

	@Override
	public Object valueof(Object value) {
		return Integer.valueOf(value.toString());
	}
	
	@Override
	public Hashtable<String, String> getLibraryCode() {
		return codigosHash;
	}
	public boolean canBeImplicitConversion(String type1, String type2){
		String wrapperString ="java.lang.Integer";
		String nativeString ="int";
		if ((wrapperString.equals(type1)&& nativeString.equals(type2) || (wrapperString.equals(type2)&& nativeString.equals(type1))))
		{
			return true;
		}
		return false;
	}
}
